
import { ref, watch, reactive, nextTick } from 'vue';
export const useEditModal = (props: any, formStateClone: any, handlerData: any) => {
    const visible = ref(false);
    const formRef: any = ref(null);
    const formState: any = reactive(Object.assign({}, formStateClone));
    watch(
        () => visible.value,
        () => {
            if (visible.value) {
                nextTick(() => {
                    formRef.value.resetFields();
                    if (props.data?.id) {
                        const data = handlerData ? handlerData(props.data) : props.data
                        console.log('data', data);

                        // 修改
                        Object.keys(data).forEach((key) => {
                            formState[key] = data[key];
                        });
                    } else {
                        // 新增
                        Object.keys(formState).forEach(key => {
                            formState[key] = formStateClone[key];
                        });
                        console.log('formState', formState);

                    }
                });
            }
        },
    );

    return {
        visible,
        formRef,
        formState
    }

}